﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace UI
{
    public class UILuckCirclePanel : MonoBehaviour
    {
        public enum CircleCount
        {
            Two=2,
            Three=3,
            Four=4,
            Six=6,
            Eight=8,
            Twelve= 12,
        }

        public CircleCount circleCount = CircleCount.Two;

        #region 模块
        public GameObject templateTwo;   //2s
        public GameObject templateThree; //3s
        public GameObject templateFour;  //4s
        public GameObject templateSix;   //6s
        public GameObject templateEight; //8s
        public GameObject templateTwelve;//12s
        #endregion

        #region 脚本
        UILuckCircleTemplate script;
        #endregion

        #region 数据
        public string guid;
        public string stringIds;
        public string luckId;
        public List<bool> lightUps;
        public int count;
        public List<UIHeroLuck> lucks;
        #endregion
        
        /// <summary>
        /// 中心英雄
        /// </summary>
        /// <param name="guid"></param>
        public void Init(string guid)
        {
            this.guid = guid;
            
            InitLucksData();
            EnableTemplatePrefab();
            ActivedIcons();
        }
       

        void InitLucksData()
        {
            var heros = StaticData.playerData.InstPlayerData.BattleCamps;
            foreach(var it in heros)
            {
                if (it.Value!=null && 
                    it.Value.hero!=null && 
                    it.Value.hero.guid == guid)
                {
                    lucks = it.Value.lucks;
                }
            }
            
            count = lucks.Count;
        }
        
        void DisableTemplatePrefab()
        {
            templateTwo.SetActive(false);
            templateThree.SetActive(false);
            templateFour.SetActive(false);
            templateSix.SetActive(false);
            templateEight.SetActive(false);
            templateTwelve.SetActive(false);
        }

        void EnableTemplatePrefab()
        {
            DisableTemplatePrefab();

            circleCount = (CircleCount)count;
            switch (circleCount)
            {
                case CircleCount.Two:
                    templateTwo.SetActive(true);
                    script = templateTwo.GetComponent<UILuckCircleTemplateTwo>();
                    break;
                case CircleCount.Three:
                    templateThree.SetActive(true);
                    script = templateThree.GetComponent<UILuckCircleTemplateThree>();
                    break;
                case CircleCount.Four:
                    templateFour.SetActive(true);
                    script = templateFour.GetComponent<UILuckCircleTemplateFour>();
                    break;
                case CircleCount.Six:
                    templateSix.SetActive(true);
                    script = templateSix.GetComponent<UILuckCircleTemplateSix>();
                    break;
                case CircleCount.Eight:
                    templateEight.SetActive(true);
                    script = templateEight.GetComponent<UILuckCircleTemplateEight>();
                    break;
                case CircleCount.Twelve:
                    templateTwelve.SetActive(true);
                    script = templateTwelve.GetComponent<UILuckCircleTemplateTwelve>();
                    break;
            }
#if UNITY_EDITOR
            if(!(circleCount == CircleCount.Two || 
                 circleCount == CircleCount.Four || 
                 circleCount == CircleCount.Three||
                 circleCount == CircleCount.Eight || 
                 circleCount == CircleCount.Six ||
                 circleCount == CircleCount.Twelve ))
            {
                Debug.LogErrorFormat("[ERROR] 少了数据 缘分ID:"+luckId +" 缘分总数:" +lucks.Count);          
            }
#endif
            script.Init(lucks);
        }

      
        void ActivedIcons()
        {
            var activeds = new List<bool>();

            var heroLucks = new List<UIHeroLuck>();
            var treaLucks = new List<UIHeroLuck>();
            var equipLucks = new List<UIHeroLuck>();
            
            foreach (var luck in lucks)
            {
                if (luck.luckType == Story.LuckType.Hero) heroLucks.Add(luck);
                if (luck.luckType == Story.LuckType.Treasure) treaLucks.Add(luck);
                if (luck.luckType == Story.LuckType.Equipment) equipLucks.Add(luck);
            }

            heroLucks.Sort((lh, rh) => { return rh.Weight - lh.Weight; });

            foreach (var luck in heroLucks)
            {
                luck.FreshLuckActived();
                activeds.Add(luck.IsActived);
            }
            foreach (var luck in equipLucks)
            {
                luck.FreshLuckActived();
                activeds.Add(luck.IsActived);
            }
            foreach (var luck in treaLucks)
            {
                luck.FreshLuckActived();
                activeds.Add(luck.IsActived);
            }
            script.Actived(activeds);


            bool hasActived = false;
            foreach(var b in activeds)
            {
                if(b)
                {
                    hasActived = true;
                    break;
                }
            }

            //设置中心的激活状态
            List<bool> actived = new List<bool>();
            actived.Add(hasActived);
            foreach(var it in script.LuckIcons)
            {
                if(it.center)
                {
                    it.SetActived(actived);
                }
            }
        }
    }
}